package cn.itcast.bos.service.base.imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierDao;
import cn.itcast.bos.dao.base.Fixed_AreaDao;
import cn.itcast.bos.dao.base.TakeTimeDao;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.Fixed_AreaService;

@Service
@Transactional
public class Fixed_AreaServiceImp implements Fixed_AreaService {

	@Autowired
	private Fixed_AreaDao fixedAreaDao;

	@Override
	public void save(FixedArea area) {
		fixedAreaDao.save(area);
	}

	@Override
	public Page<FixedArea> findAll(Specification<FixedArea> spec, Pageable pageable) {
		return fixedAreaDao.findAll(spec, pageable);
	}

	@Override
	public void delBtach(String[] split) {

		for (String id : split) {
			fixedAreaDao.delete(id);
		}

	}

	@Autowired
	TakeTimeDao takeDao;
	@Autowired
	CourierDao courDao;

	@Override
	public void associactionCourierToFiexedArea(FixedArea fixedArea, Integer takeTimeId, Integer courierId) {

		// 先查出各个对象的持久化对象
		FixedArea fixar = fixedAreaDao.findOne(fixedArea.getId());
		TakeTime takeTime = takeDao.findOne(takeTimeId);
		Courier courier = courDao.findOne(courierId);
		// 快递员关联到定区
		fixar.getCouriers().add(courier);
		// 收派时间关联快递员
		courier.setTakeTime(takeTime);
	}

}
